package com.artron.mediaartron.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.artron.baselib.adapter.recyclerview.BaseViewHolder;
import com.artron.baselib.adapter.recyclerview.ItemViewDelegate;
import com.artron.baselib.adapter.recyclerview.MultiItemTypeAdapter;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.entity.ShoppingCartItemData;
import com.artron.mediaartron.util.TextSpanUtils;

import java.util.List;

import static com.artron.mediaartron.data.production.FrameConstant.BLANK_CUSTOM_MADE;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-31.
 */

public class ShoppingCartAdapter extends MultiItemTypeAdapter<ShoppingCartItemData> {

    private float mMeasuredHeight;

    public interface OnPriceChangeListener {

        void onPriceChanged(View view, BaseViewHolder holder, int position, int number);

        void onCheckChanged(View view, BaseViewHolder holder, int position, boolean isCheck);
    }

    private OnPriceChangeListener mOnPriceChangeListener;

    public ShoppingCartAdapter(Context context, List<ShoppingCartItemData> data) {
        super(context, data);
        addItemViewDelegate(new ShoppingCartTitleDelegate());
        addItemViewDelegate(new ShoppingCartItemDelegate());
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final BaseViewHolder holder = super.onCreateViewHolder(parent, viewType);
        FrameLayout frameLayout = holder.getView(R.id.ItemShoppingCart_fl_frame);
        if (frameLayout != null && mMeasuredHeight == 0) {
            int height = frameLayout.getLayoutParams().height;
            int spec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
            frameLayout.measure(spec, spec);
            mMeasuredHeight = frameLayout.getMeasuredHeight();
        }


        View viewMinus = holder.getView(R.id.ItemShoppingCart_tv_number_minus);
        if (viewMinus != null) {
            viewMinus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    ShoppingCartItemData item = mDatas.get(position);
                    int number = addOrMinus(-1, item, (TextView) holder.getView(R.id.ItemShoppingCart_tv_number));
                    if (mOnPriceChangeListener != null) {
                        mOnPriceChangeListener.onPriceChanged(v, holder, position, number);
                    }
                }
            });
        }
        View viewAdd = holder.getView(R.id.ItemShoppingCart_tv_number_add);
        if (viewAdd != null) {
            viewAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    ShoppingCartItemData item = mDatas.get(position);
                    int number = addOrMinus(1, item, (TextView) holder.getView(R.id.ItemShoppingCart_tv_number));
                    if (mOnPriceChangeListener != null) {
                        mOnPriceChangeListener.onPriceChanged(v, holder, position, number);
                    }
                }
            });
        }

        CheckBox rb = holder.getView(R.id.ItemShoppingCart_rb_checked);
        if (rb != null) {
            rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int position = holder.getAdapterPosition();
                    mDatas.get(position).setChecked(isChecked);
                    if (mOnPriceChangeListener != null) {
                        mOnPriceChangeListener.onCheckChanged(buttonView, holder,
                                position, isChecked);
                    }
                }
            });
        }

        return holder;
    }

    public void setOnPriceChangeListener(OnPriceChangeListener onPriceChangeListener) {
        mOnPriceChangeListener = onPriceChangeListener;
    }

    private int addOrMinus(int per, ShoppingCartItemData item, TextView tvNumber) {
        String number = tvNumber.getText().toString();
        int intNumber = Integer.parseInt(number);
        if (intNumber != 1 || per > 0) {
            intNumber += per;
            tvNumber.setText(String.valueOf(intNumber));
        }
        item.setQuantity(intNumber);
        return intNumber;
    }

    private class ShoppingCartTitleDelegate implements ItemViewDelegate<ShoppingCartItemData> {
        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_shopping_cart_title;
        }

        @Override
        public boolean isForViewType(ShoppingCartItemData item, int position) {
            return item.isTitle();
        }

        @Override
        public void convert(BaseViewHolder holder, ShoppingCartItemData data, int position) {
            ((TextView) holder.itemView).setText(data.getTitle());
        }

        @Override
        public boolean isEnable() {
            return false;
        }
    }

    private class ShoppingCartItemDelegate implements ItemViewDelegate<ShoppingCartItemData> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_shopping_cart_content;
        }

        @Override
        public boolean isForViewType(ShoppingCartItemData item, int position) {
            return !item.isTitle();
        }

        @Override
        public void convert(BaseViewHolder holder, ShoppingCartItemData data, int position) {
            ImageView imageView = holder.getView(R.id.ItemShoppingCart_iv_image);
            float width, height;
            if (!TextUtils.isEmpty(data.getWorksSize())) {
                String[] sizeArr = data.getWorksSize().split("X");
                width = Float.parseFloat(sizeArr[0]);
                height = Float.parseFloat(sizeArr[1]);
            } else {
                width = height = 500;
            }
            float per = mMeasuredHeight / Math.max(width, height);
            float layoutWidth = (width - BLANK_CUSTOM_MADE) * per;
            float layoutHeight = (height - BLANK_CUSTOM_MADE) * per;
            ViewGroup.LayoutParams params = imageView.getLayoutParams();
            params.height = (int) layoutHeight;
            params.width = (int) layoutWidth;
            imageView.setLayoutParams(params);

            holder.setText(R.id.ItemShoppingCart_tv_name, data.getWorksName())
                    .setText(R.id.ItemShoppingCart_tv_number, data.getQuantity() + "")
                    .setText(R.id.ItemShoppingCart_tv_size, data.getWorksSize() + "mm")
                    .setText(R.id.ItemShoppingCart_tv_type, data.getWorksColor() + data.getWorksShape())
                    .setChecked(R.id.ItemShoppingCart_rb_checked, data.isChecked())
                    .setImageUrl(R.id.ItemShoppingCart_iv_image, data.getWorksThumbnailUrl() + "");

            TextView tvPrice = holder.getView(R.id.ItemShoppingCart_tv_price);
            TextSpanUtils.setTextWithSpan(tvPrice,
                    TextSpanUtils.TextSpan.holder("￥", new AbsoluteSizeSpan(11, true)),
                    TextSpanUtils.TextSpan.holder(data.getPrice() + ".", new AbsoluteSizeSpan(15, true)),
                    TextSpanUtils.TextSpan.holder("00", new AbsoluteSizeSpan(11, true)));
        }

        @Override
        public boolean isEnable() {
            return false;
        }
    }
}
